package com.kumz.SpringMVC.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//登录拦截器
public class LoginInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler)throws Exception{
        String uri = request.getRequestURI();
        System.out.println("当前请求的地址:"+uri);
        if(request.getSession().getAttribute("USER_SESSION") != null || uri.contains("/login")){
            System.out.println(uri+"放行");
            return true;
        }else{
            request.getRequestDispatcher("/WEB-INF/pages/login/showLogin.jsp").forward(request,response);
            return false;
        }
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)throws Exception{
    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response,Object handler,Exception ex)throws Exception{
    }

}
